<?php
namespace app\admin\controller;

use app\common\controller\CommonBase;
use app\admin\model\Variable as SiteConfig;

use think\Request;
use think\Loader;
use think\Url;


class Website extends CommonBase
{
    public function varList2()
    {
        $varList = SiteConfig::all();

        return json($varList);
    }


    public function varList($type = 'globals')
    {
        if(!in_array($type,['globals','index','group']))
        {
            return $this->error('分类不存在!');
        }
        //查询操作应当是静态调用
        $varList = SiteConfig::all(function($query) use($type){
                $query->where('admin_type',$type)->order('rank desc,id asc');
            });
        $this->assign('varList',$varList);
        $this->assign('pageConfig', json_encode([
                'tab_name'  =>  $type,
                'up_url'   =>  Url::build('update'),
            ]));
        return $this->fetch();
    }

	public function config()
	{
        //获取参数列表
		$config = SiteConfig::webConfig()->select();

        //参数列表过滤重组

		return (json($config));
	}


    public function add()
    {
        $request = Request::instance();
        if ($request->isPost()) {
            $params = $request->param();

            if (loader::validate('Variable')->scene('add')->check($params) === false) {
                return $this->error(loader::validate('Variable')->getError());
            }

            if (($key = Loader::model('Variable')->addVariable($params)) === false) {
                return $this->error(loader::model('Variable')->getError());
            }

            Loader::model('BackstageLog')->record("添加自定义变量：[{$key}]");

            return $this->success('自定义变量添加成功', Url::build('admin/website/index'));

        }
        return '123';

        //$this->assign('inputTypes', Db::table('variable_type')->select());

        //return $this->fetch();
    }


    public function update()
    {
        $request = Request::instance();

        if ($request->isPost()||1) {
            $params = $request->param();

            //动态加载验证规则
            return json(['status'=>1,'t'=>'123']);

            return json_encode(Loader::model('Variable')->updateList($params));
            //dump(Loader::model('Variable')->updateList($params));
            //return json_encode(['rel'=>1]);
        }
    }


    public function editForm()
    {
        return $this->fetch();
    }

}